package cn.jingzhuan.element.demo.view.util

import android.content.Context
import android.content.SharedPreferences
import android.util.Log


object SharedWrapper{

    private val TAG = "SharedWrapper"
    private lateinit var sp : SharedPreferences
    private lateinit var name : String

    fun with (context: Context,name: String) :SharedWrapper {
//        if (sp == null || name == null || !this.name.equals(name)) {
//            this.name = name
//            sp = context.getSharedPreferences(name,Context.MODE_PRIVATE)
//        }
        sp = context.getSharedPreferences(name,Context.MODE_PRIVATE)
        return this
    }

    fun getBoolean(key: String, defValue: Boolean): Boolean {
        val value = sp.getString(key, "false")
        if ("true".equals(value!!, ignoreCase = true) || "false".equals(value!!, ignoreCase = true)) {
            return java.lang.Boolean.parseBoolean(value)
        } else {
            if (value != null) {
                Log.e(TAG, "Parse boolean error -> $key : $value")
            }
            return defValue
        }
    }

    fun setBoolean(key: String, value: Boolean) {
        sp.edit().putString(key, java.lang.Boolean.toString(value)).apply()
    }


}


